import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
    path: "/",
    redirect: "/login"
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/Login.vue")
  },
  {
    path: "/main",
    name: "main",
    redirect: "/home",
    component: () => import("@/views/Main.vue"),
    children: [{
        path: "/home",
        name: "home",
        component: () => import("@/views/home/Home.vue")
      },
      {
        path: "/list",
        name: "list",
        meta:{
          active:"list"
        },
        beforeEnter: (to, from, next) => {
          const user = sessionStorage.getItem("user")
          if (user) {
            next()
          } else {
            next("/login")
          }

        },
        redirect: "/films/nowPlaying",
        component: () => import("@/views/list/List.vue"),
        children:[
          {
            path: "/films/nowPlaying",
            name: "now",
            meta:{
              active:"now",
              pactive:"list"
            },
            component: () => import("@/views/list/Now.vue"),
          },
          {
            path: "/films/comingSoon",
            name: "coming",
            meta:{
              active:"coming",
              pactive:"list"
            },
            component: () => import("@/views/list/Coming.vue"),
          }
        ]
      },
      {
        path: "/news",
        name: "news",
        // 独享的路由前置守卫
        beforeEnter: (to, from, next) => {
          const user = sessionStorage.getItem("user")
          if (user) {
            next()
          } else {
            next("/login")
          }

        },
        component: () => import("@/views/news/News.vue")
      },
      {
        path: "/mine",
        name: "mine",
        beforeEnter: (to, from, next) => {
          const user = sessionStorage.getItem("user")
          if (user) {
            next()
          } else {
            next("/login")
          }

        },
        component: () => import("@/views/mine/Mine.vue")
      },
    ]
  }

]

const router = new VueRouter({
  routes
})


const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

// 全局路由守卫
// router.beforeEach((to, from, next) => {

//   if(to.path == "/login") {
//     return next();
//   }
//   const user = sessionStorage.getItem("user")
//   if(user) {
//     next()
//   }else {
//     next("/login")
//   }

// })

export default router